package pro.cherish.quartzlockdemo.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pro.cherish.quartzlockdemo.da.GiveBack;
import pro.cherish.quartzlockdemo.entity.STimetaskLog;
import pro.cherish.quartzlockdemo.service.STimetaskLogService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/admin/task")
@Slf4j
@Api(tags = "日志接口")
public class TimetaskLogController {
    @Autowired
    STimetaskLogService sTimetaskLogService;
    @Autowired
    STimetaskLogService sTimetaskService;

    @RequestMapping(value = "/getLog", produces = "text/html;charset=UTF-8;")
    @ResponseBody
    public GiveBack getLog(HttpServletRequest request, HttpSession httpSession) {
        String code = "";
        String message = "";
        String data = null;
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        int pageNum = 0;
        int pageSize = 0;
        try {
            pageNum = Integer.parseInt(pageNumStr);
            pageSize = Integer.parseInt(pageSizeStr);
        } catch (Exception e) {
            code = "100";
            message = "类型转换异常";
            return GiveBack.make(code, message, data);
        }
        try {
            QueryWrapper<STimetaskLog> qw = new QueryWrapper<>();
            qw.orderByDesc("create_date");
            IPage<STimetaskLog> page = sTimetaskLogService.page(new Page<>(pageNum, pageSize), qw);
            List<STimetaskLog> list = page.getRecords();
            JSONObject json = new JSONObject();
            json.put("pages", page.getPages());
            json.put("totals", page.getTotal());
            message = json.toString();
            JSONArray arr = new JSONArray();
            if (list.size() == 0) {
                data = "";
            } else {
                for (int i = 0; i < list.size(); i++) {
                    JSONObject json2 = new JSONObject();
                    STimetaskLog s = list.get(i);
                    json2.put("createDate", s.getCreateDate().toLocaleString());
                    json2.put("name", sTimetaskService.selectByPrimaryKey(s.getJobId()).getName());
                    json2.put("result", s.getState());
                    arr.add(json2);
                }
            }
            code = "0";
            data = JSONUtils.toJSONString(arr);
            return GiveBack.make(code, message, data);
        } catch (Exception e) {
            code = "-1";
            message = "系统异常";
            return GiveBack.make(code, message, data);
        }
    }
}